var txtAnswer;
var current_dict_id;

var Consts = {
	HINT_PART : 0.25,
	MOTHER_WEIGHT : 0.5
}

var Fields = {};

window.onload = function() {
	Fields.dicts = document.getElementById("user_dicts");
	Fields.question = document.getElementById("question_button");
	Fields.answer = document.getElementById("answer_button");
	eventUtility.addEvent(Fields.question, "click", nextQuestion);	
	
	// jeigu useris turi žodynų, tai pirmasis iš jų nustatomas
	// kaip einamasis, paleidus programą.
	if (!Fields.dicts || Fields.dicts.length === 0) {
		return;
	}
	Fields.dicts.selectedIndex = 0; 
	// ateityje padaryti, kad parinktų išsaugotą kūkyje
	eventUtility.addEvent(Fields.dicts, "change", nextQuestion);
	nextQuestion();
}

function nextQuestion() {
	if (!Fields.dicts || Fields.dicts.length === 0) {
		return;
	}
	//console.log("passed");
	var dict_id = Fields.dicts.value;
    ajaxGet("/wordage/items/fetchrandomitem?di=" + dict_id,
		    function() {
		        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
					setQuestion(JSON.parse(xmlhttp.responseText));
		        }
		    }
    );
}

function setQuestion(item) {
	// clear former answer
	Fields.answer.innerHTML = "&nbsp;";
	
	// choose which - mother or fore - goes as a question,
	// and which goes as an answer
	if (Math.random() < Consts.MOTHER_WEIGHT) {
		Fields.question.innerHTML = item.mother;
		txtAnswer = item.fore;
	}
	else {
		Fields.question.innerHTML = item.fore;
		txtAnswer = item.mother;
	}
	
	// attach event listeners
	eventUtility.addEvent(Fields.answer, "click", showAnswer);
	//eventUtility.addEvent(Fields.answer.parentNode, "click", showHint);
}
	
function showAnswer() {
	Fields.answer.innerHTML = txtAnswer;
	eventUtility.removeEvent(Fields.answer.parentNode, "dblclick", showAnswer);
	eventUtility.removeEvent(Fields.answer.parentNode, "click", showHint);
}


// neapsimoka. nieko tas neduoda
function showHint () {
	Fields.answer.innerHTML=createHint(txtAnswer);
	eventUtility.removeEvent(Fields.answer.parentNode, "click", showHint);
}

//neapsimoka
function createHint(txt) {
	var  text = txt;
	text = text.replace (/\([^\(]*\)/g, ""); // išmeta, kas skliausteliuose (xxxx)
	text = text.replace (/\[[^\[]*\]/g, ""); // išmeta, kas laužtiniuose skliaustuose [xxxx]
	text = text.replace (/[\s_]+/g, " "); // pasikartojančius tarpus arba pabraukimus pakeičia vienu tarpu
	text = text.replace (/^[\W]+|[\W]+$/g, ""); // išmeta kas ne raidės ir ne skaitmenys pradžioje ir pabaigoje
	// alert (txt + " - " + text);
	
	if  (text.length * Consts.HINT_PART < 0.5) {
		return "&nbsp;";
	}
	var  hintLength = Math.round(text.length * Consts.HINT_PART);
	var lastIndex = Math.floor((Math.random() * text.length) + 1);
	var firstIndex = lastIndex - hintLength;
	var hintText = text.substring(firstIndex, lastIndex);
	
	if (firstIndex > 0) {
		hintText = "..." + hintText;
	}
	
	if (lastIndex < txtAnswer.length) {
		hintText = hintText + "...";
	}
	
	return hintText;	
}
